<template>
	<view>
		<div class='setBox'>
			<u--form
				labelPosition="left"
				labelWidth='150'
				labelAlign='center'
				:label-style="{'font-weight':'600','font-size':'30rpx'}"
				:model="model"
				:rules="rules"
				ref="form1">
				<u-form-item
					label="请输入当前登录密码"
					prop="userInfo.oldpass"
					borderBottom
					ref="item1">
					<u--input
						v-model="model.userInfo.oldpass"
						border="none"
						placeholder="请输入当前登录密码"
						fontSize='13'
						maxlength='6'
					></u--input>
				</u-form-item>
				<u-gap height="12" bgColor="#F4F4F4"></u-gap>
				<u-form-item
					label="确认新密码"
					prop="userInfo.newpass"
					borderBottom
					ref="item2">
					<u--input
						v-model="model.userInfo.newpass"
						border="none"
						placeholder="确认新密码"
						fontSize='13'
						maxlength='6'
					></u--input>
				</u-form-item>
			</u--form>
			<div class='btn'>
				<u-button color='#2ED8C4' text="确定" @click='changePass'></u-button>
			</div>
		</div>
	</view>
</template>

<script>
	export default{
		data(){
			return{
				userdata:[],
				password:null,
				userID:null,
				model: {
					userInfo: {
						oldpass: '',
						newpass: ''
					},
				},
				rules: {
					'userInfo.oldpass': [
						{
							type: 'any',
							required: true,
							message: '请输入旧密码',
							trigger: ['blur', 'change']
						},
						{
							// 自定义验证函数，见上说明
							validator: (rule, value, callback) => {
								// 上面有说，返回true表示校验通过，返回false表示不通过
								// uni.$u.test.mobile()就是返回true或者false的
								// return uni.$u.test.mobile(value);
								if(value != this.password){
									return false
								}else{
									true
								}
							},
							message: '当前登录密码不正确',
							// 触发器可以同时用blur和change
							trigger: ['change','blur'],
						}
					],
					'userInfo.newpass': {
						type: 'any',
						required: true,
						message: '请输入新密码',
						trigger: ['blur', 'change']
					},
				},
			}
		},
		onLoad() {
			this.userdata = uni.getStorageSync("userInfo");
			this.password = this.userdata.password
			this.userID = this.userdata.id
		},
		methods:{
			changePass(){
				this.$api.DLQupdatePassword({id:this.userID,password:this.model.userInfo.newpass}).then((res)=>{
					// console.log(res)
					if(res.code == 200){
						uni.reLaunch({
							url:'/pages/Center/Set/success'
						})
					}else{
						uni.showToast({
							title:'修改失败',
							icon:"error"
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	page{
		background-color: #F4F4F4;
	}
	.setBox{
		background-color: #fff;
		margin-top: 16upx;
		// padding: 0 20upx;
		.btn{
			background-color: #F4F4F4 ;
			padding: 40upx;
		}
	}
</style>